
use std::rc::Rc;
use druid::widget::{ Label };
use druid::{Affine, WidgetPod};
use druid_widget_nursery::{Tree, TreeNode};
use keepasst_core::{camphor::Camphor, core::group::Group};
use druid::widget::prelude::*;

use crate::app::Taxonomy;

pub struct MTree {
    tree: WidgetPod<Taxonomy, Tree<Taxonomy>>,
}

impl MTree {
    pub fn new() -> Self {
        MTree {
            tree: WidgetPod::new(Tree::new(|t: &Taxonomy| {
                let name_str = t.name.as_str();
                println!("tree new name : {}", name_str);
                Label::new(name_str)
            })),
        }
    }
}

impl Widget<Rc<Group>> for MTree {
    fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut Rc<Group>, _env: &Env) {

    }

    fn lifecycle(&mut self, _ctx: &mut LifeCycleCtx, _event: &LifeCycle, _data: &Rc<Group>, _env: &Env) {}

    fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: &Rc<Group>, _data: &Rc<Group>, _env: &Env) {}

    fn layout(
        &mut self,
        _layout_ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        _data: &Rc<Group>,
        _env: &Env,
    ) -> Size {
        bc.max()
    }

    fn paint(&mut self, ctx: &mut PaintCtx, data: &Rc<Group>, env: &Env) {
        let tax = Taxonomy::from(Rc::clone(data));
        println!("tax: {:?}", tax);
        self.tree.paint(ctx, &tax, env);
    }
}